package org.springframework.remoting.jsonrpc.test;

import junit.framework.Assert;
import junit.framework.TestCase;

import org.jabsorb.JSONRPCBridge;
import org.jabsorb.JSONSerializer;
import org.jabsorb.serializer.UnmarshallException;
import org.jabsorb.serializer.impl.ArraySerializer;
import org.jabsorb.serializer.impl.BeanSerializer;
import org.jabsorb.serializer.impl.BooleanSerializer;
import org.jabsorb.serializer.impl.DateSerializer;
import org.jabsorb.serializer.impl.DictionarySerializer;
import org.jabsorb.serializer.impl.ListSerializer;
import org.jabsorb.serializer.impl.MapSerializer;
import org.jabsorb.serializer.impl.NumberSerializer;
import org.jabsorb.serializer.impl.SetSerializer;
import org.jabsorb.serializer.impl.StringSerializer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.remoting.jsonrpc.ClassSerializer;
import org.springframework.remoting.jsonrpc.ObjectSerializer;
import org.springframework.remoting.jsonrpc.PrimitiveSerializer;

public class JSONTestCase extends TestCase {

	ApplicationContext ctx;
	
	public JSONTestCase() {
		super();
	}

	public JSONTestCase(String arg0) {
		super(arg0);
	}
	public void testInt()
	{
		JSONRPCBridge bridge = new JSONRPCBridge();
		JSONTestService service = (JSONTestService) ctx.getBean("jsonService");
		int i = Integer.MAX_VALUE;
		int j = service.testInt(i);
		Assert.assertTrue(i==j);
	}
	public void testChar()
	{
		JSONRPCBridge bridge = new JSONRPCBridge();
		JSONTestService service = (JSONTestService) ctx.getBean("jsonService");
		char i = 'a';
		char j = service.testChar(i);
		Assert.assertTrue(i==j);
	}
	public void testDouble()
	{
		JSONRPCBridge bridge = new JSONRPCBridge();
		JSONTestService service = (JSONTestService) ctx.getBean("jsonService");
		double i = 1.1d;
		double j = service.testDouble(i);
		Assert.assertTrue(i==j);

	}

	public void testFloat()
	{
		JSONRPCBridge bridge = new JSONRPCBridge();
		JSONTestService service = (JSONTestService) ctx.getBean("jsonService");
		float i = Float.MAX_VALUE;
		float j = service.testFloat(Float.MAX_VALUE);
		Assert.assertTrue(i==j);

	}
	public void testLong()
	{
		JSONRPCBridge bridge = new JSONRPCBridge();
		
		JSONTestService service = (JSONTestService) ctx.getBean("jsonService");
		long i = 9l;//Long.MIN_VALUE;
		long j = service.testLong(i);
		Assert.assertTrue(i==j);
	}
	public void testShort()
	{
		JSONRPCBridge bridge = new JSONRPCBridge();
		
		JSONTestService service = (JSONTestService) ctx.getBean("jsonService");
		short i = Short.MIN_VALUE;
		short j = service.testShort(i);
		Assert.assertTrue(i==j);

	}
	public void testString()
	{
		JSONRPCBridge bridge = new JSONRPCBridge();
		
		JSONTestService service = (JSONTestService) ctx.getBean("jsonService");
		String i = "a";
		String j = service.testString(i);
		Assert.assertTrue(i.equals(j));

	}
	public void test2Strings()
	{
		JSONRPCBridge bridge = new JSONRPCBridge();
		
		JSONTestService service = (JSONTestService) ctx.getBean("jsonService");
		String a = "a";
		String b = "b";
		String result = service.testString(a,b);
		Assert.assertTrue((a+b).equals(result));

	}
	
	public void testStringArray()
	{
	  JSONRPCBridge bridge = new JSONRPCBridge();
	  JSONTestService service = (JSONTestService) ctx.getBean("jsonService");
	  String[] strings = {"a","b","c d"};
	  String[] result = service.testStringArray(strings);
	  
	  Assert.assertTrue(result.length==3);
		
	}
	
	public void testArray()
	{
		JSONSerializer serializer = new JSONSerializer();
		try {
			serializer.registerSerializer(new BeanSerializer());
			serializer.registerSerializer(new ArraySerializer());
			serializer.registerSerializer(new DictionarySerializer());
			serializer.registerSerializer(new MapSerializer());
			serializer.registerSerializer(new SetSerializer());
			serializer.registerSerializer(new ListSerializer());
			serializer.registerSerializer(new DateSerializer());
			serializer.registerSerializer(new StringSerializer());
			serializer.registerSerializer(new NumberSerializer());
			serializer.registerSerializer(new BooleanSerializer());
			serializer.registerSerializer(new PrimitiveSerializer());
			serializer.registerSerializer(new ClassSerializer());
			serializer.registerSerializer(new ObjectSerializer());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			String[] array = (String[])serializer.fromJSON("[\"a\",\"b\",\"c d\"]");
			Assert.assertTrue(array.length == 3);
		} catch (UnmarshallException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Assert.fail(e.toString());
		}
		
		
		
	}
	protected void setUp() throws Exception {
		super.setUp();
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
		        new String[] {"/org/springframework/remoting/jsonrpc/test/json.xml"});
		this.ctx = (ApplicationContext) ctx;
	}
	
	

}
